package controllers;

import com.fasterxml.jackson.databind.JsonNode;
import dao.DateReport_Repository;
import play.data.FormFactory;
import play.libs.Json;
import play.libs.concurrent.HttpExecutionContext;
import play.mvc.Controller;
import play.mvc.Result;

import javax.inject.Inject;
import java.util.concurrent.CompletionStage;

public class DateReportController extends Controller {
    private final FormFactory formFactory;
    private final DateReport_Repository dateReport_repository;
    private final HttpExecutionContext ec;

    @Inject
    public DateReportController(FormFactory formFactory, DateReport_Repository dateReport_repository, HttpExecutionContext ec) {
        this.formFactory = formFactory;
        this.dateReport_repository = dateReport_repository;
        this.ec = ec;
    }

    public CompletionStage<Result> getDateReport(){
        JsonNode json = request().body().asJson();
        String StartDate = json.findPath("StartDate").textValue();
        String EndDate = json.findPath("EndDate").textValue();
        String HWMC = json.findPath("HWMC").textValue();
        String KHMC = json.findPath("KHMC").textValue();


        return dateReport_repository.getDateReport(StartDate, EndDate, HWMC, KHMC).thenApplyAsync(_GetListByProc -> {
            return ok(Json.toJson(_GetListByProc));
        }, ec.current());
    }

    public CompletionStage<Result> getReportDept(){
        JsonNode json = request().body().asJson();
        String StartDate = json.findPath("StartDate").textValue();
        String EndDate = json.findPath("EndDate").textValue();
        String HWMC = json.findPath("HWMC").textValue();

        return dateReport_repository.getReportDept(StartDate, EndDate, HWMC).thenApplyAsync(_GetListByProc -> {
            return ok(Json.toJson(_GetListByProc));
        }, ec.current());
    }
}
